/**
 * Created by caelan on 2018/10/29.
 */

/*加载图层*/
var loadLayer = {
  /*创建或清空图层*/
  initLayer: function (name) {
    var layers = Global.map.getLayersByName( name );
    if( layers.length > 0 ){
      layers[0].removeAllFeatures();
    } else {
      layers.push( new SuperMap.Layer.Vector( name ) );
      Global.map.addLayer( layers[0] );
      Global.selectFeatureLayers.push( layers[0] );
      Global.selectFeature.setLayer( Global.selectFeatureLayers );
    }
    return layers;
  },
  /*关闭其他图层，无参数则关闭所有图层*/
  closeOtherLayer: function (current) {
    Config.allMapLayer.filter(function (val) { return val !== current }).forEach(function (val) {
      var layers = Global.map.getLayersByName(val);
      layers.forEach(function (lay) {
        if ( lay.features ){
          Global.selectFeatureLayers = Global.selectFeatureLayers.filter(function (f) { return f.name !== val });
          lay.removeAllFeatures();
        }
        Global.map.removeLayer(lay)
      });
    });
  },

  /*色斑图*/
  geoLayer: function (name, data) {
    var layers = this.initLayer(name);
    this.closeOtherLayer(name);

    var colors = data[1].items,
        datas = data[2].features, features = [];

    /*函数式编程୧(๑•̀⌄•́๑)૭*/
    datas.forEach(function (regionItem) {
      regionItem.geometry.coordinates.forEach(function (polygon) {
        var multiPolygon = polygon.map(function (line) {
          /*边界点*/
          var points = line.map(function (point) {
            return new SuperMap.Geometry.Point( point[0],point[1] );
          });
          /*点连线*/
          return new SuperMap.Geometry.LinearRing( points );
        });
        /*用线生成岛洞图*/
        var region = new SuperMap.Geometry.Polygon(multiPolygon),
          regionVector = new SuperMap.Feature.Vector(region),
          MGMinValue = regionItem.properties.MGMinValue,
          MGMaxValue = regionItem.properties.MGMaxValue;
        /*填色*/
        MGMinValue = MGMinValue <= -90 ? MGMaxValue : MGMinValue;
        for ( var l = colors.length - 1; l >= 0;l-- ) {
          if ( MGMinValue >= colors[l].Value ) {
            regionVector.style = {
              fillColor:colors[l].FillStyle.ForeColor,
              fillOpacity:1,
              strokeWidth:0
            };
            break;
          }
        }
        features.push(regionVector);
      });
    });

    layers[0].addFeatures(features);

  },

  /*点图*/
  pointLayer: function (name, displayField) {
    var layers = this.initLayer(name);
    this.closeOtherLayer(name);

    var tableData = tablePage.resultData,
        stations = customizeStation.station;

    var fs = tableData.map(function (val) {

      var l = stations.filter(function (s) {
        return s['V01000'] === val['站号'];
      })[0];

      /*点颜色*/
      var color = '#fff';
      for (var i in Global.legendColor){
        if ( Number(Global.legendColor[i].value) >= val[displayField] ){
          color = Global.legendColor[i].color;
          break;
        }
      }

      var f = new SuperMap.Feature.Vector( new SuperMap.Geometry.Point(l['V06001'], l['V05001']) );
      
      f.style = {
        label: String( val[displayField] ),
        labelYOffset: 15,
        fontWeight: "bold",
        fontSize: 13,
        fontFamily: "微软雅黑",
        fontStyle: "italic",
        fillColor: color,
        strokeColor: '#000',
        strokeWidth: 1,
        pointRadius: 5
      };
      f.info = {
        id: name,
        type: 'basic',
        content: {'名称': val['名称'], '站号': val['站号'], '所属市区': val['市'],
          '时间': val['时间'] || val['最小值时间'] || val['最大值时间'] || val['平均值时间']}
      };
      /*展示字段*/
      f.info.content[tablePage.tableHeadField[tablePage.tableEnglishField.indexOf(displayField)]] = val[displayField];

      return f
    });

    layers[0].addFeatures(fs);
  },

  /*色斑图 + 点图*/
  geoPointLayer: function () {

  }
};